import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:hugeicons/hugeicons.dart';
import '../../resources.dart';
import 'file/file.dart';
import 'mine/mine_page.dart';
import 'table/table.dart';
import 'home/home.dart';

/// created by hcl
/// on 2024/2/27
/// MainPage
/// desc
class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<StatefulWidget> createState() => MainState();
}

class MainState extends State<MainPage> with TickerProviderStateMixin {
  int currentIndex = 0;
  String title = word.text_title;

  late List<MainPageData> pages;

  late PageController pageController;
  late TabController tabController;

  // final _noScreenshot = NoScreenshot.instance;

  @override
  void initState() {
    super.initState();
    pages = [
      MainPageData(
          0, word.text_title, HugeIcons.strokeRoundedHome03, const HomePage()),
      MainPageData(
          1, word.file_title, HugeIcons.strokeRoundedTask01, const FilePage()),
      MainPageData(
          2, word.table_title, HugeIcons.strokeRoundedTable, const TablePage()),
      MainPageData(3, word.mine, HugeIcons.strokeRoundedUser, const MinePage()),
    ];
    pageController = PageController();
    tabController = TabController(length: pages.length, vsync: this);
    tabController.addListener(() {
      pageController.jumpToPage(tabController.index);
    });

    WidgetsBinding.instance.addPostFrameCallback((callback) {
      setBrightness();
    });
    // _noScreenshot.screenshotOff();
  }

  setBrightness() {
    Future.delayed(const Duration(milliseconds: 100), () {
      SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          statusBarIconBrightness: Brightness.light));
    });
  }

  toOrder() {
    tabController.animateTo(1);
  }

  @override
  void didUpdateWidget(covariant MainPage oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorUtils.color_FFFFFF,
      resizeToAvoidBottomInset: false,
      body: isWeb
          ? Container(alignment: Alignment.topCenter, child: buildContent())
          : buildContent(),
    );
  }

  Widget buildContent() {
    return SizedBox(
      width: designWidth.ss,
      child: Column(
        children: [
          Expanded(
            child: PageView.builder(
              controller: pageController,
              itemCount: pages.length,
              itemBuilder: (context, index) {
                return pages[index].page;
              },
              onPageChanged: (index) {
                if (index == 0) {}
                currentIndex = index;
                title = pages[index].title;
                if (mounted) setState(() {});
              },
              physics: const NeverScrollableScrollPhysics(),
            ),
          ),
          Container(
            decoration: const BoxDecoration(
                color: ColorUtils.color_FFFFFF,
                border: Border(
                    top: BorderSide(
                        color: ColorUtils.color_B1B1B1, width: 0.5))),
            child: TabBar(
              controller: tabController,
              isScrollable: false,
              unselectedLabelColor: ColorUtils.color_777777,
              unselectedLabelStyle: normal012,
              labelColor: ColorUtils.app_color,
              labelStyle: mediumAD12,
              labelPadding: const EdgeInsets.symmetric(horizontal: 0),
              indicatorSize: TabBarIndicatorSize.label,
              indicatorWeight: 2,
              indicatorColor: Colors.transparent,
              tabs: pages.map((e) => buildBottomWidget(e)).toList(),
            ),
          )
        ],
      ),
    );
  }

  Widget buildBottomWidget(MainPageData pageData) {
    return Center(
      child: Container(
        padding: EdgeInsets.only(top: 8.ss,bottom: 5.ss),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            HugeIcon(
                icon: pageData.icon,
                size: currentIndex == pageData.index ? 24.ss : 20.ss,
                color: currentIndex == pageData.index
                    ? ColorUtils.app_color
                    : ColorUtils.color_777777),
            Text(pageData.title)
          ],
        ),
      ),
    );
  }
}

class MainPageData {
  int index;
  String title;
  IconData icon;
  Widget page;

  MainPageData(this.index, this.title, this.icon, this.page);
}
